// netlib.hpp

#ifndef _KS_INC_NETLIB_HPP_
#define _KS_INC_NETLIB_HPP_

#include "netlib.h"

namespace ks {

	bool net_address_make(struct sockaddr* addr, const char* ip, unsigned short port)
		{ return ks_net_address_make(addr, ip, port)!=FALSE; }

	bool net_address_make(struct sockaddr* addr, const char* ip_port)
		{ return ks_net_address_make1(addr, ip_port)!=FALSE; }

	class NetChannel {
	public:
		static NetChannel* cast(KSNetChannel* channel) { return (NetChannel*)channel; }
		operator KSNetChannel* () { return &channel_; }
	public:
		KSNetChannelStatus status() const	{ return channel_.status; }
		int err() const						{ return channel_.err; }
		int fd() const						{ return channel_.fd; }
		bool working() const				{ return ks_net_channel_is_working(&channel_); }
	public:
		bool connect_sync(KSNetEventDispatcher* dp, const struct sockaddr* addr)
			{ return ks_net_channel_connect_sync(&channel_, dp, addr)!=FALSE; }

		bool connect_async(KSNetEventDispatcher* dp, const struct sockaddr* addr)
			{ return ks_net_channel_connect_async(&channel_, dp, addr)!=FALSE; }

		bool connect_finish(KSNetEventDispatcher* dp, int fd)
			{ return ks_net_channel_connect_finish(&channel_, fd)!=FALSE; }

		bool write_all(const void* buf, size_t len, const void* header_buf=0, size_t header_len=0)
			{ return ks_net_channel_write_all_full(&channel_, buf, len, header_buf, header_len)!=FALSE; }

		size_t write(const void* buf, size_t len)
			{ return ks_net_channel_write(&channel_, buf, len); }

		void flush()
			{ ks_net_channel_flush(&channel_); }

		void close(int status=KS_NET_CHANNEL_ST_CLOSED, int err=0)
			{ ks_net_channel_close_full(&channel_, status, err); }

		void close_wait_peer()
			{ ks_net_channel_close_wait_peer(&channel_); }

	private:
		KSNetChannel	channel_;
	};

} // ks

#endif//_KS_INC_NETLIB_HPP_

